import {Actions} from "react-native-router-flux";
import { QueryList, QueryDetail, Submit } from '../services/examination'

export default {
  namespace: 'examinationDetail',

  state: {
    businesId: '',
    detail: [],
    agree: true,
    opinion: '',
    dialogText:'',
  },

  subscriptions: {
  },

  effects: {
    *submit({payload}, {call, put, select}){

      const { userId } = yield select(state => state.login)
      const { businesId, opinion, agree } = yield select(state => state.examinationDetail)
      let obj = {businesId, opinion, state: agree ? '1' : '2', examine: userId}

      const { json, error } = yield call(Submit, obj, 'POST')

      if(error || json.code === '0'){
        console.log('Submit Error: ' + error)
      }else{
        // if (opinion == ''){
        //   yield put({type:'thisDialog/thisInit'});
        //   yield put({type:'dialogShow',payload:'意见不能为空！'});
        // }else {
          yield put({type:'examination/query'})
          yield put({type:'indexPage/getQueryList'})
          Actions.pop()
        // }
      }

    }
  },

  reducers: {
    queryDetailSuccess(state, {payload}){
      console.log(payload,'detail')
      let taskRecordId = payload.taskRecordId

      let list = []
      list.push({label: '项目名称', problemArr:[],value: payload.project.prjName,arr:[]})
      list.push({label: '检查日期', problemArr:[],value: payload.inspectDate,arr:[]})
      list.push({label: '监督员', problemArr:[],value: payload.supervisor,arr:[]})
      list.push({label: '检查部位', problemArr:[],value: payload.inspectPosition,arr:[]})
      list.push({label: '发现问题', problemArr:payload.changeFormProblems,value:'',arr: []})
      list.push({label: '严重等级', problemArr:[],value: payload.seriousGrade,arr:[]})
      list.push({label: '处理结果', problemArr:[],value: payload.handleResult,arr:[]})
      list.push({label: '整改期限', problemArr:[],value: payload.changeDate,arr:[]})
      list.push({label: '整改要求', problemArr:[],value: payload.changeRequirement,arr:[]})
      list.push({label: '单位签字', problemArr:[],value:'',arr: payload.changeFormCorpSigns})
      list.push({label: '记录编号', problemArr:[],value: payload.task.taskNum === ''?'暂无':payload.task.taskNum,arr:[]})
      list.push({label: '企业名称', problemArr:[],value: payload.corp.corpName,arr:[]})

      return {...state, detail: list, businesId: payload.id}
    },

    changeAgree(state){
      return { ...state, agree: !state.agree}
    },

    changeOpinion(state, {payload}){
      return { ...state, opinion: payload }
    },
    dialogShow(state,{payload}){
      return {...state,dialogText:payload}
    },
      getClean(state,{payload}){
        return {...state,opinion:''}
      }
  },
}